using System;
using Commons.Models.Definitions;

namespace Commons.Models.Scores
{
	public class HighScore
	{
		public HighScoreDefinition Definition { get; protected set; }
		public int CurrentScore { get; set; }
		public int LastScore { get; set; }
		public int BestScore { get; set; }
		public DateTime LastUpdate { get; set; }

		public HighScore(HighScoreDefinition definition, DateTime lastUpdate, int currentScore, int lastScore, int bestScore)
		{
			this.Definition = definition;
			this.LastUpdate = lastUpdate;
			this.CurrentScore = currentScore;
			this.LastScore = lastScore;
			this.BestScore = bestScore;
		}

		public void Add(int score, DateTime time)
		{
			CheckPeriod(time);

			this.CurrentScore += score;
			this.LastUpdate = time;

			if (CurrentScore > BestScore)
			{
				BestScore = CurrentScore;
			}
		}

		public bool CheckPeriod(DateTime time)
		{
			if (time > LastUpdate && !Definition.Periodicity.IsSamePeriod(time, LastUpdate))
			{
				LastScore = CurrentScore;
				CurrentScore = 0;

				return true;
			}
			else
			{
				return false;
			}
		}
	}
}

